package com.example.demolearn_2.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demolearn_2.R;
import com.example.demolearn_2.config.CommApiConstant;
import com.example.demolearn_2.util.OkHttp3Util;
import com.example.demolearn_2.util.ShowImageUtils;
import com.example.demolearn_2.util.ToastHelper;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DetailActivity extends AppCompatActivity {

    private ImageView m_ivPost;
    private TextView m_tvContent;
    private TextView m_tvTitle;

    private Context m_Context;
    Handler m_handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:{
                    Bundle data = msg.getData();
                    ShowImageUtils.showImageView(m_Context,0,data.getString("picUrl"),m_ivPost);
                    break;
                }
                case 2: {
                    Bundle data = msg.getData();
                    m_tvContent.setText(data.getString("content"));
                    break;
                }
                default:break;

            }
        } ;
    };
    private String cityName;
    private int cityId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        setContentView(R.layout.activity_detail);
        m_Context = this;
        initView();
        initData();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        String item = bundle.getString("item");
        String[] rets = item.split(";");

        if(rets.length >= 2){
            cityId = Integer.parseInt(rets[0]);
            cityName = rets[1];
            m_tvTitle.setText(cityName);
        }

        OkHttp3Util.doGet(CommApiConstant.ChinaEveryDayPicUrlApi, null, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                ToastHelper.show(m_Context,"ChinaEveryDayPicUrlApi:failed");
                Log.e("OkHttp3Util",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //ToastHelper.show(mContext,response.body().string());
                String picUrl = response.body().string();
                Message message = new Message();
                message.what = 1;
                Bundle data = new Bundle();
                data.putString("picUrl",picUrl);
                message.setData(data);
                m_handler.sendMessage(message);
            }
        });


        OkHttp3Util.doGet(CommApiConstant.ChinaProvincesGetApi+"/"+cityId, null, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                ToastHelper.show(m_Context,"ChinaEveryDayPicUrlApi:failed");
                Log.e("OkHttp3Util",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //ToastHelper.show(mContext,response.body().string());
                String content = response.body().string();
                Message message = new Message();
                message.what = 2;
                Bundle data = new Bundle();
                data.putString("content",content);
                message.setData(data);
                m_handler.sendMessage(message);
            }
        });
    }

    private void initView() {
        m_ivPost = findViewById(R.id.iv_post);
        m_tvContent = findViewById(R.id.tv_content);
        m_tvTitle = findViewById(R.id.tv_title);
    }
}